package net.jlibrary.common.project.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.fasterxml.jackson.annotation.JsonManagedReference;

import net.jlibrary.common.core.annotation.CodeGenComment;
import net.jlibrary.common.core.annotation.PropertyVoPo;
import net.jlibrary.common.core.annotation.PropertyVoPoCondition;
import net.jlibrary.common.core.annotation.SearchCondition;
import net.jlibrary.common.core.annotation.SearchField;
import net.jlibrary.common.core.entity.BaseObject;
import net.jlibrary.common.core.entity.IValid;

/**
 * 权限对应的功能
 * 
 * @author sunlf
 *
 */
@Entity(name = "tab_permission_function")
@BatchSize(size = 15)
public class PermissionFunction extends BaseObject implements IValid {
	private static final long serialVersionUID = -3838535034460191696L;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@SearchField(value = "id", condition = SearchCondition.EQUAL)
	private Long id;
	@Column(length = 50, unique = true)
	@SearchField(value = "code", condition = SearchCondition.EQUAL)
	@CodeGenComment(value="功能编码" ,viewList=true,viewSearch=true,required=true,minLength=3,maxLength=50)
	private String code;// 功能编码
	@Column(length = 50)
	@SearchField(value = "name", condition = SearchCondition.FUZZ_RIGHT)
	@CodeGenComment(value="功能名称" ,viewList=true,viewSearch=true,viewText=true,required=true,minLength=3,maxLength=50)
	private String name;// 功能名称
	@Column(length = 200)
	@CodeGenComment(value="功能描述" ,viewList=true,minLength=0,maxLength=200)
	private String description;// 功能描述
	@OneToMany(mappedBy = "function", targetEntity = PermissionUrl.class, fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	@BatchSize(size = 15)
	@OrderBy("url")
	@CodeGenComment(value="功能地址列表" ,viewList=true,required=true)
	@PropertyVoPo(value=PropertyVoPoCondition.Collection2String,split="\r\n",children="url")
	@JsonManagedReference
	private Set<PermissionUrl> urls = new HashSet<PermissionUrl>();// 对应的地址列表
	@CodeGenComment(value="有效性" ,viewList=true,viewSearch=true,required=true)
	private boolean valid;
	@Transient
	private boolean allowQueryInvalid;
	@Transient
	private boolean compellingDelete;
	@Transient
	@SearchField(value = "valid", condition = SearchCondition.EQUAL ,type="Boolean")
	private String validString;
	@Column(updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	@CodeGenComment(value = "创建时间", viewList = true)
	private Date createdate;
	@Temporal(TemporalType.TIMESTAMP)
	@CodeGenComment(value = "最后修改时间")
	private Date lastdate;

	public Long getId() {
		return id;
	}
	
	@Override
	public Serializable getPrimaryKey() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Set<PermissionUrl> getUrls() {
		return urls;
	}

	public void setUrls(Set<PermissionUrl> urls) {
		this.urls = urls;
	}

	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	public boolean isAllowQueryInvalid() {
		return allowQueryInvalid;
	}

	public void setAllowQueryInvalid(boolean allowQueryInvalid) {
		this.allowQueryInvalid = allowQueryInvalid;
	}

	public boolean isCompellingDelete() {
		return compellingDelete;
	}

	public void setCompellingDelete(boolean compellingDelete) {
		this.compellingDelete = compellingDelete;
	}

	public Date getCreatedate() {
		return createdate;
	}

	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}

	public Date getLastdate() {
		return lastdate;
	}

	public void setLastdate(Date lastdate) {
		this.lastdate = lastdate;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((code == null) ? 0 : code.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PermissionFunction other = (PermissionFunction) obj;
		if (code == null) {
			if (other.code != null)
				return false;
		} else if (!code.equals(other.code))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	public String getValidString() {
		return validString;
	}

	public void setValidString(String validString) {
		this.validString = validString;
	}
}
